package net.jueb.sniff4j.extr.filter.blocker;
import io.netty.channel.Channel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import net.jueb.sniff4j.extr.base.AbstractRuntime;
import net.jueb.sniff4j.extr.base.ServerContext;
import net.jueb.sniff4j.extr.base.UserContext;

/**
 * 抽象拦截接口
 * @author Administrator
 * 返回null则终止数据发送
 */
public abstract class Blocker {
	protected static final InternalLogger log = InternalLoggerFactory.getInstance(Blocker.class);
	private Channel fromChannel;
	private Channel toChannel;
	private AbstractRuntime runtime;
	private ServerContext serverContext;
	private UserContext userContext;
	
	/**
	 * 此对象成员必须在创建前完成初始化，因为子类要使用相关方法
	 * @param target
	 * @param runtime
	 * @param fromChannel
	 * @param toChannel
	 */
	public Blocker(AbstractRuntime runtime,Channel fromChannel, Channel toChannel) {
		this.runtime=runtime;
		this.serverContext=runtime.getServerConext();
		this.fromChannel=fromChannel;
		this.toChannel=toChannel;
		this.userContext=findUserContext();
	}
	
	private UserContext findUserContext()
	{
		UserContext ctx=null;
		ctx=this.runtime.getCurrentUserContext(fromChannel);
		if(ctx==null)
		{
			ctx=this.runtime.getCurrentUserContext(toChannel);
		}
		return ctx;
	}
	
	protected final Channel getFromChannel() {
		return fromChannel;
	}

	protected final Channel getToChannel() {
		return toChannel;
	}

	protected final AbstractRuntime getRuntime() {
		return runtime;
	}

	protected final ServerContext getServerContext() {
		return serverContext;
	}
	
	protected final UserContext getUserContext() {
		return userContext;
	}
	/**返回通过阻断器的阻断目标
	 * 默认直接返回目标，不阻断
	 * @return 返回null则阻断终止发送
	 */
	public  Object cross(Object blockTarget)
	{
		return blockTarget;
	}
}
